<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Canvas Draw Line</title>
</head>
<body>
    <canvas id="myCanvas" width="400" height="400" style="border:1px solid #000;"></canvas>
    <script>
        function drawLineWithExtension(startX, startY, endX, endY) {
            var canvas = document.getElementById('myCanvas');
            var ctx = canvas.getContext('2d');
            // 绘制原始直线
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(endX, endY);
            ctx.strokeStyle = 'red';
            ctx.stroke();
            // 计算反向延长线的坐标
            var extensionEndX = startX - (endX - startX);
            var extensionEndY = startY - (endY - startY);
            console.log(extensionEndX, extensionEndY);
            console.log(endX, endY);
         
            // 绘制反向延长线
            ctx.beginPath();
            ctx.moveTo(startX, startY);
            ctx.lineTo(extensionEndX, extensionEndY);
            ctx.strokeStyle = 'black';
            ctx.stroke();
        }

        // 调用函数并传入起始坐标和结束坐标
        drawLineWithExtension(200, 200,400, 200);
    </script>
</body>
</html>
